package code.extract;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fang Lu, fanglupku@gmail.com
 */

public class CodePageDealer extends BasePageDealer{
	public static void main(String args[])
	{
		
		long t1 = System.currentTimeMillis();
		try {
//			String text = FileDealer.getFileContent("C:\\Users\\ftc\\Desktop\\代码块提取\\sample_page\\1_wellho.txt", "GBK");
			String text = FileDealer.getFileContent("codepagesample\\hostiwise.txt", "GBK");
			ArrayList<SegmentItem> tList = CodeExtractor.extractCodeSegmentFromPage(text, 
					CodeExtractor.CONTINUAL_MERGE|CodeExtractor.SMALL_CODE_MERGE
					| CodeExtractor.EMPTY_MERGE);
			
			for (int i=0; i<tList.size(); ++i)
			{
//				if (!CodeJudge.isCode(tList.get(i).getContent()))
//				{
//					continue;
//				}
				System.out.println(i + "+-----------------" + tList.get(i).getBeginTag());
				System.out.println("==" + tList.get(i).getIndex());
				System.out.println(tList.get(i).getContent());
				System.out.println(i + "*-----------------" + tList.get(i).getEndTag());
				System.out.println(i + "spam above-----------------" + tList.get(i).isSpamSegment());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long t2 = System.currentTimeMillis();
		System.out.println("time:" + (t2 - t1));
	}
	public static ArrayList<String> getTextFragmentInOneTag(String originString, String tag)
	{
		ArrayList<String> resultList = new ArrayList<String>();
		ArrayList<String> rawList = extractOneTagContent(originString, tag);
		for (int i=0; i<rawList.size(); ++i)
		{
			if (rawList.get(i) != null)
			{
				String thisFragment = eraseAllTags(rawList.get(i));
				resultList.add(new String(thisFragment));
			}
		}
		return resultList;
	}
	
	public static ArrayList<SegmentItem> getTextFragmentInPage(String originString, ArrayList<String> segTagList)
	{
		ArrayList<SegmentItem> resultList = new ArrayList<SegmentItem>();
		ArrayList<String> borderList = new ArrayList<String>();
		ArrayList<Integer> segPosList = new ArrayList<Integer>();
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<segTagList.size(); ++i)
		{
			if (i != 0)
			{
				sb.append("|");
			}
			sb.append(segTagList.get(i));
		}
		String str_pattern = "<(/{0,1})(\\s*?)(" + sb.toString() + ")((\\s*?)|(\\s+?.*?))(/{0,1})>";
//		System.out.println(str_pattern);
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
					| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		segPosList.add(new Integer(0));
		borderList.add("<begin>");
		while (matcher.find())
		{
//			if (matcher.end() - matcher.start() > 2)
			{
				segPosList.add(new Integer(matcher.start()));
				String thisBorder = new String(originString.substring(matcher.start(), matcher.end()));
//				thisBorder = thisBorder.replaceAll("/", "");
//				thisBorder = thisBorder.replaceAll("(\\s*?)|(\\s+?.*?)", "");
				borderList.add(thisBorder);
			}
			
//			System.out.println(matcher.start() + ":" + new String(originString.substring(matcher.start(), matcher.end())));
		}
		for (int i=0; i<segPosList.size(); ++i)
		{
			if ( i < segPosList.size() - 1)
			{
				String rawText = new String(originString.substring(segPosList.get(i), segPosList.get(i+1)));
				boolean isSpam = SpamFilter.isSpamItem(rawText);
				SegmentItem thisItem = new SegmentItem(new String(eraseAllTags(rawText)), borderList.get(i), borderList.get(i+1), i);
				if (isSpam){
					thisItem.setSpamType();
				}
				resultList.add(thisItem);
			}
			else
			{
				String rawText = new String(originString.substring(segPosList.get(i)));
				boolean isSpam = SpamFilter.isSpamItem(rawText);
				SegmentItem thisItem = new SegmentItem(new String(eraseAllTags(rawText)), borderList.get(i), "<end>", i);
				if (isSpam){
					thisItem.setSpamType();
				}
				resultList.add(thisItem);
			}
		}
		return resultList;
	}
	public static boolean isEmptyText(String text){
		String temp = text.trim();
		temp = temp.replaceAll("\\s", "");
//		temp = temp.replaceAll("\\n", "");
		temp = temp.replaceAll("　", "");
		if (temp.length() > 0){
			return false;
		}
		return true;
	}
	public static ArrayList<String> filterEmptyText(ArrayList<String> src, ArrayList<String> dest){
		if (dest == null){
			dest = new ArrayList<String>();
		}
		if (src != null){
			for (int i=0; i<src.size(); ++i){
				if (isEmptyText(src.get(i)) == false){
					dest.add(src.get(i));
				}
			}
		}
		return dest;
	}
	public static boolean isEmptySegmentItem(SegmentItem seg){
		String temp = seg.getContent().trim();
		temp = temp.replaceAll("\\s", "");
//		temp = temp.replaceAll("\\n", "");
		temp = temp.replaceAll("　", "");
		if (temp.length() > 0){
			return false;
		}
		return true;
	}
	public static ArrayList<SegmentItem> filterEmptySegmentItem(ArrayList<SegmentItem> src, ArrayList<SegmentItem> dest){
		if (dest == null){
			dest = new ArrayList<SegmentItem>();
		}
		if (src != null){
			for (int i=0; i<src.size(); ++i){
				if (isEmptySegmentItem(src.get(i)) == false){
					dest.add(src.get(i));
				}
			}
		}
		return dest;
	}
	/**
	 * merge empty item to next nearest item
	 * @param src
	 * @param dest
	 * @return
	 */
	public static ArrayList<SegmentItem> mergeEmptySegmentItem(ArrayList<SegmentItem> src, ArrayList<SegmentItem> dest){
		if (dest == null){
			dest = new ArrayList<SegmentItem>();
		}
		if (src != null){
			StringBuffer sb = new StringBuffer();
			for (int i=0; i<src.size(); ++i){
				if (isEmptySegmentItem(src.get(i)) == false){
					sb.append(src.get(i).getBeginTag());
					src.get(i).setBeginTag(sb.toString());
					dest.add(src.get(i));
					sb.delete(0, sb.length());
				}else{
					sb.append(src.get(i).getBeginTag());
					sb.append(src.get(i).getEndTag());
				}
			}
		}
		return dest;
	}
	
}